import asyncio
import base64
import time

import flet as ft

from dataTemp.user import User
from process.copyBWJ import CopyBWJ
from ui.addRole import addRole, Role, Role_ui
from ui.login import login

brick_roles = []


# gold_roles=[]
def newBWJ(e):
    print("开始")
    CopyBWJ(local_user)


def run_main_ui(user: User):
    global local_user
    local_user = user
    ft.app(target=MainUI)


def image_to_base64(image_path):
    try:
        with open(image_path, "rb") as image_file:
            # 读取图片文件并编码为 base64
            encoded_string = base64.b64encode(image_file.read())
            return encoded_string.decode('utf-8')
    except:
        print(6)


def MainUI(page: ft.Page):
    page.title = "dnf手游搬砖机器人"
    local_img = ft.Image(src_base64=image_to_base64(f"./runs/detect/predict/image0.jpg"), width=page.width * 2 / 3)

    async def updadeimg():
        while True:
            local_img.src_base64 = image_to_base64(f"./runs/detect/predict/image0.jpg")
            await asyncio.sleep(0.1)
            local_img.update()

    page.run_task(updadeimg)
    # 每秒更新图像
    page.add(ft.Row([
        ft.Container(width=page.width * 2 / 3, height=page.height - 20,
                     content=ft.Container(
                         content=ft.Column([
                             local_img,
                             ft.Container(width=60, height=45,
                                          content=ft.Text("开始", size=20, weight=ft.FontWeight.W_700,
                                                          color=ft.colors.RED_50),
                                          bgcolor=ft.colors.BLUE,
                                          alignment=ft.alignment.center, margin=ft.margin.only(top=5, left=770),
                                          on_click=newBWJ),
                             ft.Container(content=ft.Column([
                                 ft.Text("公告"), ft.Container(content=ft.Text("公告内容"),
                                                               width=650, height=117,
                                                               border_radius=10,
                                                               border=ft.Border(left=ft.BorderSide(width=1),
                                                                                top=ft.BorderSide(width=1),
                                                                                right=ft.BorderSide(width=1),
                                                                                bottom=ft.BorderSide(width=1)))
                             ], spacing=0)),
                         ], spacing=0, alignment=ft.MainAxisAlignment.CENTER)
                     )),
        # 右边
        ft.Container(width=page.width * 1 / 3 - 30, height=page.height - 20,
                     content=ft.Column([
                         ft.Container(content=ft.Row([
                             ft.Column([
                                 ft.Container(content=ft.Text("vpi会员")),
                                 ft.Container(content=ft.Text("里古拉斯张三")), ], spacing=0,
                                 horizontal_alignment=ft.CrossAxisAlignment.END),
                             ft.Container(
                                 content=ft.Image(src="../tasks/headIco/01.jpg"),
                                 width=30,
                                 height=30,
                                 border_radius=15,  # 半径设置为一半，以形成圆形
                                 clip_behavior=ft.ClipBehavior.ANTI_ALIAS  # 确保裁剪为圆形
                             )], spacing=0, alignment=ft.MainAxisAlignment.END)),
                         ft.Divider(),  # 添加一条水平线
                         ft.Container(content=MC(page, "搬砖角色", local_user.brick_roles)),
                         ft.Container(content=MC(page, "摸金角色", local_user.gold_roles)),
                     ], spacing=2))
    ]))
    # page.vertical_alignment = ft.VerticalAlignment.START
    # page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
    # login(page)


class MC(ft.Container):
    def __init__(self, page: ft.Page, tile: str, roles):
        super().__init__()
        lv = ft.ListView(
            expand=1,
            # width=120,
            padding=3,
            spacing=5,
            controls=[Role_ui(page, roles, r) for r in roles]
        )
        self.content = ft.Column([
            ft.Row([
                ft.Text(tile),
                ft.ElevatedButton("添加角色", on_click=lambda e: addRole(page, roles, lambda: print("回调更新")))
            ], alignment=ft.MainAxisAlignment.SPACE_BETWEEN),
            ft.Container(width=390, height=270, border_radius=15, border=ft.Border(left=ft.BorderSide(width=2),
                                                                                   top=ft.BorderSide(width=2),
                                                                                   right=ft.BorderSide(width=2),
                                                                                   bottom=ft.BorderSide(width=2)),
                         content=lv)
        ], spacing=0)


if __name__ == '__main__':
    ft.app(target=MainUI)
    # main_yl()
    pass
